package com.cn.wanxi.service.impl;

import com.cn.wanxi.iomodel.IOCompnayModel;
import com.cn.wanxi.iomodel.IONavModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.INavService;
import com.cn.wanxi.util.CommonPath;
import com.cn.wanxi.util.IO;
import com.cn.wanxi.util.MyMethod;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NavServiceImpl implements INavService {


    @Override
    public List<NavModel> getNavModelList() {

        /**
         * 1.写
         * 2.读
         * 3.封装
         */
        writeNote();
        return setModelList(IO.readNote(CommonPath.NAV_NAME.getValue()));
    }

    private List<NavModel> setModelList(String result) {
        String[] results = result.split(CommonPath.AA.getValue());
        String tempNames = results[1].split("=")[1];
        tempNames = MyMethod.replace(tempNames);
        String tempHrefs = results[0].split("=")[1];
        tempHrefs = MyMethod.replace(tempHrefs);
        List<NavModel> navModelList = new ArrayList<>();//定义一个集合的空间
        String[] names = tempNames.split(",");//静态赋值
        String[] hrefs = tempHrefs.split(",");//动态赋值
        for (int i = 0; i < names.length; i++) {
            NavModel model = new NavModel();
            model.setName(names[i]);
            model.setHref(hrefs[i]);
            navModelList.add(model);//添加导航对象
        }
        return navModelList;
    }

    private void writeNote() {
        List<String> list = new ArrayList<>();
        IONavModel ioNavModel = new IONavModel();
        list.add("hrefs=" + Arrays.toString(ioNavModel.hrefs));
        list.add("names=" + Arrays.toString(ioNavModel.names));
        IO.tempWrite(list);
    }


}
